package com.yax.mickey.controller;

import com.yax.mickey.common.result.Result;
import com.yax.mickey.entity.User;
import com.yax.mickey.pojo.dto.UserLoginDTO;
import com.yax.mickey.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
@Api(tags = "用户端用户相关接口")
@Slf4j
public class UserController {
	@Autowired
	private UserService userService;

	@PostMapping("/login")
	@ApiOperation("用户登录")
	public Result<User> login(@RequestBody User user) {
		User loginUser = userService.login(user);
		return Result.success(loginUser);
	}

	@GetMapping("/getUserByUsername/")
	@ApiOperation("根据用户名查询用户信息")
	public Result<User> getUserByUsername(String username) {
		User user = userService.getUserByUsername(username);
		return Result.success(user);
	}

	@PostMapping("/register")
	@ApiOperation("用户注册")
	public Result<String> register(@RequestBody UserLoginDTO userLoginDTO) {
		User user = userService.getUserByUsername(userLoginDTO.getUsername());
		if (user == null) {
			//没有占用
			//注册
			userService.register(userLoginDTO.getUsername(),userLoginDTO.getPassword());
			return Result.success("用户注册成功！");
		}else {
			//占用
			return Result.error("用户名已存在！");
		}
	}

	@PostMapping("/addUser")
	@ApiOperation("新增用户")
	public Result<Integer> addUser(@RequestBody User user) {
		Integer adduser = userService.addUser(user);
		return Result.success(adduser);
	}

	@PutMapping("/updateUser")
	@ApiOperation("更新用户信息")
	public void updateUser(@RequestBody User user) {
		userService.updateUser(user);
	}
}
